package com.livsdesign.toolsetdemo

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.google.android.material.button.MaterialButton
import com.livsdesign.tools.condition.*

class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {

    val blueAdapter: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

    private val bluetoothBtn: MaterialButton by lazy {
        findViewById(R.id.bluetoothBtn)
    }

    private val locationBtn: MaterialButton by lazy {
        findViewById(R.id.locationBtn)
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bleDrawable =
            ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp) ?: return
        val ble =
            ConditionTask(bleDrawable, "ble", "ble", Template.Bluetooth(this))
        bluetoothBtn.setOnClickListener {
            ble.template.request()
        }

        val location = ConditionTask(
            bleDrawable,
            "location",
            "location",
            Template.LocationToggle(this)
        )

        locationBtn.setOnClickListener {
            location.template.request()
        }

        ble.template.mState.observe(this, {
            bluetoothBtn.text = "${it.name} BLE"
        })

        location.template.mState.observe(this) {
            locationBtn.text = it.name
        }
    }
}